package cn.damai.controller.web;

import cn.damai.model.Order;
import cn.damai.model.OrderLinkUser;
import cn.damai.order.model.OrderDTO;
import cn.damai.order.service.OrderService;
import cn.damai.model.User;
import cn.damai.util.DubboUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 订单控制层
 */
@Controller
@RequestMapping("order")
public class OrderController {

    @Reference(url = DubboUtil.ORDER_URL,version = DubboUtil.VRESION,group = DubboUtil.GROUP)
    private OrderService orderService;

//   测试区start===========================
    @RequestMapping("test")
    public String toOrder(){

        return "test/orderTest";
    }

    @RequestMapping("test/session")
    public String testRuq(@RequestParam("uId")Long uId, Order order, HttpSession session){
        User user = new User();
        user.setId(77L);
        session.setAttribute("user",user);
        session.setAttribute(user.getId()+"",user.getId());
        session.setAttribute("order",order);
        session.setAttribute("uId",uId);
        return "test/orderTest";
    }

    @RequestMapping("test/confirm")
    public String testGetRq(HttpSession session){
        Order order = (Order) session.getAttribute("order");
        Long uId = (Long) session.getAttribute("uId");
        User user = (User) session.getAttribute("user");
        Long userId = (Long) session.getAttribute(user.getId() + "");
        System.out.println("userId 77 ? :"+ userId);
        System.out.println(order.getSchedulerId()+"---"+uId);
        return "index";
    }

    @GetMapping("test/orderList")
    @ResponseBody
    public List<OrderDTO> testOrderList(@RequestParam("userId") Long userId){
        List<OrderDTO> orderDTOS = orderService.orderListByUserId(userId);
        return orderDTOS;
    }

//   测试区end===========================

//    预下单,订单准备,获取座位信息,不写入sql
    @GetMapping("multi/confirm")
    public String multiConfirm(Order order, @RequestParam("areaLevel")Integer areaLevel
                                ,HttpSession session,Model model){

        User u = (User) session.getAttribute("user");

        if(u == null){
            return "redirect:/user/to/login";
        }

        order.setUserId(u.getId());
        OrderDTO orderDTO = orderService.orderReady(order,areaLevel);

        if(orderDTO == null){
            model.addAttribute("saleOver","over");
            return "user/orderConfirm";
        }

        session.setAttribute("orderReady",orderDTO);  //TODO 后期多用户登录,采用userId作为key
        model.addAttribute("orderDTO",orderDTO);
        return "user/orderConfirm";
    }


//    订单确认,写入sql
    @PostMapping("confirmOrder")
    public String confirmOrder(HttpSession session, OrderLinkUser orderLinkUser, Model model){

        User u = (User) session.getAttribute("user");

        if(u == null){
            return "redirect:/user/to/login";
        }

        OrderDTO orderDTO = (OrderDTO) session.getAttribute("orderReady");
        orderDTO.setOrderLinkUser(orderLinkUser);
        Order order = orderService.confirmOrder(orderDTO);
        model.addAttribute("order",order);
        session.removeAttribute("orderReady");
        return "user/pay";
    }

//    获取订单信息
    @RequestMapping("orderList")
    public String orderList(HttpSession session, Model model){
        User user = (User) session.getAttribute("user");

        if(user == null){
            return "redirect:/user/to/login";
        }

        List<OrderDTO> orderDTOS = orderService.orderListByUserId(user.getId());
        model.addAttribute("orderDTOS",orderDTOS);
        return "user/order";
    }

//    获取订单详情
    @GetMapping("orderDetail")
    public String orderDetail(@RequestParam("orderNo")String orderNo,Model model){
        OrderDTO orderDTO = orderService.orderDetailByOrderNo(orderNo);

        model.addAttribute("orderDetail",orderDTO);
        return "user/orderDetail";
    }

//    继续支付
    @GetMapping("payContinue")
    public String payContinue(@RequestParam("orderNo")String orderNo,Model model){
        Order order = orderService.orderInfoByOrderNo(orderNo);

        if(order == null){
            model.addAttribute("payMsg","该订单信息不存在!");
            return "user/payMsg";
        }

        return "forward:/alipay/pay?itemName=" + order.getItemName() + "&totalAmount=" + order.getTotalAmount();
    }

//    取消支付
    @GetMapping("payCancel")
    public String payCancel(@RequestParam("orderNo")String orderNo,Model model){
        boolean flag = orderService.payCancel(orderNo);

        if(flag){
            model.addAttribute("payMsg","该订单信息不存在!");
            return "user/payMsg";
        }

        return "redirect:/order/orderList";
    }

}
